def subarray_sum_equals_k(arr, k):
    cum = 0
    # base, sum 0
    mem = {0: 1}
    cnt = 0
    for num in arr:
        cum += num
        # if cum - k in mem, meaning current cum - prev cum = k, and there is x ways getting prev cum
        if cum - k in mem:
            cnt += mem[cum - k]
        if cum not in mem:
            mem[cum] = 1
        else:
            mem[cum] += 1
    return cnt

print(subarray_sum_equals_k([1,1,1], 2))
print(subarray_sum_equals_k([1,1,-1,0,-1,1,0,1,-1,1], 2))

